-- $Id: RvRStats.lua 38 2009-03-10 17:02:34Z Ermite Chevelu $

RvRStats = {}

RvRStats.MESSAGES = {}

local window = "CharacterWindowRvRStats"
local localizedMessages = {}
local oldShow = nil

-- Initialize local settings
local function initLocale()
   local language = SystemData.Settings.Language
   local switch = {
      [language.ENGLISH] = L"enUS",
      [language.FRENCH] = L"frFR",
      [language.GERMAN] = L"deDE",
      [language.ITALIAN] = L"itIT",
      [language.SPANISH] = L"esES",
      [language.KOREAN] = L"koKR",
      [language.S_CHINESE] = L"zhCN",
      [language.T_CHINESE] = L"zhTW",
      [language.JAPANESE] = L"jaJP",
      [language.RUSSIAN] = L"ruRU"
   }

   local res = switch[language.active] 
   if not res then
      res = L"enUS"
   end

   localizedMessages = RvRStats.MESSAGES[res]
end

-- return localized message
local function getLocalizedMessage(key)
    return localizedMessages[key]
end

-- return class name
local function getClassName(speciesData)
   return speciesData.name .. L" : "
end

-- return formatted kill count
local function getKillCount(speciesData)
   return StringToWString(string.format("%d", speciesData.killCount))
end

-- update RvR tab
local function updateRvRTab()
   local lifetimeKills = GameData.Player.RvRStats.LifetimeKills
   local lifetimeDeathBlows = GameData.Player.RvRStats.LifetimeDeathBlows
   local lifetimeDeaths = GameData.Player.RvRStats.LifetimeDeaths
   local KDRatio = StringToWString(string.format("%.2f" , (lifetimeKills / lifetimeDeaths)))
   local DBDRatio = StringToWString(string.format("%.2f" , (lifetimeDeathBlows / lifetimeDeaths)))
   
   local classA_Name = StringToWString("???")
   local classB_Name = StringToWString("???")
   local classC_Name = StringToWString("???")
   local classD_Name = StringToWString("???")
   local classE_Name = StringToWString("???")
   local classF_Name = StringToWString("???")
   local classG_Name = StringToWString("???")
   local classH_Name = StringToWString("???")
   local classI_Name = StringToWString("???")
   local classJ_Name = StringToWString("???")
   local classK_Name = StringToWString("???")
   local classL_Name = StringToWString("???")
   
   local classA_KillCount = StringToWString("0")
   local classB_KillCount = StringToWString("0")
   local classC_KillCount = StringToWString("0")
   local classD_KillCount = StringToWString("0")
   local classE_KillCount = StringToWString("0")
   local classF_KillCount = StringToWString("0")
   local classG_KillCount = StringToWString("0")
   local classH_KillCount = StringToWString("0")
   local classI_KillCount = StringToWString("0")
   local classJ_KillCount = StringToWString("0")
   local classK_KillCount = StringToWString("0")
   local classL_KillCount = StringToWString("0")
   
   local subTypeData
   
   -- collect imperial or chaos stats
   subTypeData = TomeGetBestiarySubTypeData( 16 ) -- Human
   if subTypeData then
      for index, speciesData in ipairs( subTypeData.species ) do
         if speciesData.isUnlocked == true then -- check if entry is unlock
            if speciesData.id == 48 or speciesData.id == 14 then -- bright wizard or choosen
               classA_Name = getClassName(speciesData)
               classA_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 49 or speciesData.id == 15 then -- kotbs or magus
               classB_Name = getClassName(speciesData)
               classB_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 50 or speciesData.id == 16 then -- warrior priest or marauder
               classC_Name = getClassName(speciesData)
               classC_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 51 or speciesData.id == 17 then -- witch hunter or zealot
               classD_Name = getClassName(speciesData)
               classD_KillCount = getKillCount(speciesData)
            end
         end
      end
   end

   -- collect dwarf or greenskin stats
   if GameData.Player.realm == GameData.Realm.DESTRUCTION then
      subTypeData = TomeGetBestiarySubTypeData( 13 ) -- Dwarf
   elseif GameData.Player.realm == GameData.Realm.ORDER then
      subTypeData = TomeGetBestiarySubTypeData( 15 ) -- Greenskin
   end
   if subTypeData then
      for index, speciesData in ipairs( subTypeData.species ) do
         if speciesData.isUnlocked == true then -- check if entry is unlock
            if speciesData.id == 42 or speciesData.id == 63 then -- engineer or shaman
               classE_Name = getClassName(speciesData)
               classE_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 43 or speciesData.id == 64 then -- slayer or squig herder
               classF_Name = getClassName(speciesData)
               classF_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 44 or speciesData.id == 92 then -- ironbreaker or black orc
               classG_Name = getClassName(speciesData)
               classG_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 45 or speciesData.id == 93 then -- runepriest or choppa
               classH_Name = getClassName(speciesData)
               classH_KillCount = getKillCount(speciesData)
            end
         end
      end
   end

   -- collect high elf or dark elf stats
   subTypeData = TomeGetBestiarySubTypeData( 14 ) -- Elf
   if subTypeData then
      for index, speciesData in ipairs( subTypeData.species ) do
         if speciesData.isUnlocked == true then -- check if entry is unlock
            if speciesData.id == 75 or speciesData.id == 31 then --archmage or witch elf
               classI_Name = getClassName(speciesData)
               classI_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 76 or speciesData.id == 32 then --white lion or disciple of khaine
               classJ_Name = getClassName(speciesData)
               classJ_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 77 or speciesData.id == 33 then --shadow warrior or blackguard
               classK_Name = getClassName(speciesData)
               classK_KillCount = getKillCount(speciesData)
            end

            if speciesData.id == 78 or speciesData.id == 34 then --swordmaster or sorcerer
               classL_Name = getClassName(speciesData)
               classL_KillCount = getKillCount(speciesData)
            end
         end
      end
   end
   
   LabelSetText(window.."Header", GameData.Player.name)
   LabelSetText(window.."Title", getLocalizedMessage("RR") .. L": " .. GameData.Player.Renown.curRank .. L"   " .. getLocalizedMessage("TITLE") .. L": " .. GameData.Player.Renown.curTitle)
   LabelSetText(window.."LifetimeK", getLocalizedMessage("LIFETIME_KILLS") .. L": " .. lifetimeKills .. L" (" .. GameData.Player.RvRStats.SessionKills .. L")")
   LabelSetText(window.."LifetimeDB", getLocalizedMessage("LIFETIME_DEATHBLOWS") .. L": " .. lifetimeDeathBlows)
   LabelSetText(window.."LifetimeD", getLocalizedMessage("LIFETIME_DEATHS") .. L": " .. lifetimeDeaths)
   LabelSetText(window.."LifetimeRatioKD", getLocalizedMessage("LIFETIME_RATIO_KILL_DEATH") .. L": " .. KDRatio)
   LabelSetText(window.."LifetimeRatioDBD", getLocalizedMessage("LIFETIME_RATIO_DEATHBLOW_DEATH") .. L": " .. DBDRatio)
   LabelSetText(window.."ClassKills", getLocalizedMessage("CLASS_KILLS"))
   
   -- Human
   LabelSetText(window.."ClassA_Name", classA_Name)
   LabelSetText(window.."ClassA_KillCount", classA_KillCount)
   
   LabelSetText(window.."ClassB_Name", classB_Name)
   LabelSetText(window.."ClassB_KillCount", classB_KillCount)
   
   LabelSetText(window.."ClassC_Name", classC_Name)
   LabelSetText(window.."ClassC_KillCount", classC_KillCount)
   
   LabelSetText(window.."ClassD_Name", classD_Name)
   LabelSetText(window.."ClassD_KillCount", classD_KillCount)
   
   -- Dwarf or Greenskin
   LabelSetText(window.."ClassE_Name", classE_Name)
   LabelSetText(window.."ClassE_KillCount", classE_KillCount)
   
   LabelSetText(window.."ClassF_Name", classF_Name)
   LabelSetText(window.."ClassF_KillCount", classF_KillCount)
   
   LabelSetText(window.."ClassG_Name", classG_Name)
   LabelSetText(window.."ClassG_KillCount", classG_KillCount)
   
   LabelSetText(window.."ClassH_Name", classH_Name)
   LabelSetText(window.."ClassH_KillCount", classH_KillCount)
   
   -- Elf
   LabelSetText(window.."ClassI_Name", classI_Name)
   LabelSetText(window.."ClassI_KillCount", classI_KillCount)
   
   LabelSetText(window.."ClassJ_Name", classJ_Name)
   LabelSetText(window.."ClassJ_KillCount", classJ_KillCount)
   
   LabelSetText(window.."ClassK_Name", classK_Name)
   LabelSetText(window.."ClassK_KillCount", classK_KillCount)
   
   LabelSetText(window.."ClassL_Name", classL_Name)
   LabelSetText(window.."ClassL_KillCount", classL_KillCount)
end

---- Init ----
function RvRStats.Init()
   initLocale()
   oldShow = CharacterWindow.OnShown
   CharacterWindow.OnShown = RvRStats.OnShown
end

function RvRStats.OnShown()
   updateRvRTab()
   oldShow()
end
